/**
 * 文件：activity_photo_detail.java
 * 包名：com.ronri.ams.sample;   --- 正式发布时需要修正
 * 说明：照片展示并支持编辑
 * 作者：MS During
 * 日期：2017/5/12
 * 历史：
 */

package com.ronri.ams.sample;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.cz.nongyetongv5.R;
import com.ronri.CustomDialog;
import com.ronri.ams.SystemEnvironmental;
import com.ronri.choosephotos.PhotoItem;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;


public class activity_photo_detail extends abstract_activity {

    // 数据列表
    private List<PhotoItem> mItems = new ArrayList<PhotoItem>();

    // 翻页上
    private Button mbtnPageUp = null;

    // 翻页下
    private Button mbtnPageDown = null;

    // 备注
    private TextView mbtnMemo = null;

    // 详情
    private TextView mbtnDetail = null;

    // 删除
    private TextView mbtnDelete = null;

    // 图片
    private ImageView mimgPhoto = null;

    // 拍照日期
    private TextView mtvDate = null;

    // 经纬度
    private TextView mtvLL = null;

    // 当前光标位置
    private int mCurrentIndex = 0;

    // 地址
    private TextView mtvAddress = null;

    // 句柄
    private Context mContext = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.control_photo_detail);
        Log.i("aaa","activity_photo_detail");
        mContext = this;

        // 读取参数
        Bundle bundle = getIntent().getExtras();

        int editLayout = bundle.getInt("edit_layout", R.layout.control_edit_box);
        String szTitle = bundle.getString("title");
        String szvTitle = bundle.getString("vtitle");
        String szPhotoJson = bundle.getString("data");
        mCurrentIndex = bundle.getInt("index", 0);

        // 设置标题
        setTitle(szTitle, szvTitle);

        // 初始化，用于控制
        init();

        // 转换数据
        mItems = JSON.parseArray(szPhotoJson, PhotoItem.class);
        // 防止无数据错误
        if(mItems == null)
            mItems = new ArrayList<PhotoItem>();

        // 照片
        mimgPhoto = (ImageView) findViewById(R.id.imgPhoto);

        // 信息
        mtvDate = (TextView)findViewById(R.id.tvDate);
        mtvLL = (TextView)findViewById(R.id.tvLL);

        // 分页
        mbtnPageUp = (Button) findViewById(R.id.btnPageUp);
        mbtnPageDown = (Button)findViewById(R.id.btnPageDown);

        // 功能按钮
        mbtnMemo = (TextView)findViewById(R.id.btnMemo);
        //mbtnDetail = (TextView)findViewById(R.id.btnDetail);
        mbtnDelete = (TextView)findViewById(R.id.btnDelete);

        // 上翻
        mbtnPageUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ((mCurrentIndex - 1) >= 0){
                    mCurrentIndex--;
                    RefreshInfo(mCurrentIndex);
                }
            }
        });

        // 下翻
        mbtnPageDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ((mCurrentIndex + 2) < mItems.size()){
                    mCurrentIndex++;
                    RefreshInfo(mCurrentIndex);
                }
            }
        });

        // 编辑备注
        mbtnMemo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, activity_control_edit_box.class);

                Bundle bundle = new Bundle();

                bundle.putInt("edit_layout", R.layout.control_edit_box_voice);
                bundle.putInt("resultCode", SystemEnvironmental.PhotoGraph_Memo_Result);
                bundle.putString("edit_value", mItems.get(mCurrentIndex).getMemo());
                bundle.putString("title","备注");
                bundle.putString("vtitle","确定");

                intent.putExtras(bundle);

                startActivityForResult(intent, SystemEnvironmental.PhotoGraph_Memo_Result);
            }
        });

        mtvAddress = (TextView)findViewById(R.id.tvaddress);

        // 显示详情
//        mbtnDetail.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//
//            }
//        });

        // 删除
        mbtnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mItems.size() == 1){
                    return;
                }

                // 提示删除
                CustomDialog.Builder builder = new CustomDialog.Builder(mContext);
                builder.setMessage("确定删除当前照片吗？(是/否)");
                builder.setTitle("信息提示");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // 移除
                        mItems.remove(mCurrentIndex);

                        // 判断是否删除了最后一个，除了点击之外
                        if(mCurrentIndex >= (mItems.size() - 1)){
                            mCurrentIndex = mItems.size() - 1;
                        }

                        Log.i("test","删除后-->"+mItems.size());
                        Log.i("test","mCurrentIndex-->"+mCurrentIndex);
                        // 刷新界面
                        RefreshInfo(mCurrentIndex);
                        dialog.dismiss();
                    }
                });

                builder.setNegativeButton("取消",
                        new android.content.DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });

                builder.create().show();
            }
        });

        // 刷新界面
        RefreshInfo(mCurrentIndex);
    }

    /***
     * 刷新数据
     */
    private void RefreshInfo(int nIndex){
        //判断是否删除仅有的一张
        if(mItems.size()==1){
            //是则返回上一页
            CancelReturn();
        }else {
            // 判断是否删除最后一张
            if(nIndex+1 >= mItems.size()){
                nIndex--;
            }

            // 获取当前信息
            PhotoItem item = mItems.get(nIndex);

            SimpleDateFormat format = new SimpleDateFormat("拍照日期：yyyy-MM-dd HH:mm:ss");
            String time =format.format(item.PhotoDate);

            mtvDate.setText(time);

            mtvLL.setText("坐标：" + item.getLatitude() + "," + item.getLongitude());
            mtvAddress.setText("拍照地址：" + item.getAddress());
            // mtvLL.setText(item.Latitude);
            mimgPhoto.setImageBitmap(item.GetBitmap());
        }
    }

    /***
     * 处理返回
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK) {
            switch (requestCode) {
                case SystemEnvironmental.PhotoGraph_Memo_Result: {
                    Bundle bundle = data.getExtras();
                    String szValue = bundle.getString("returnValue");
                    mItems.get(mCurrentIndex).Memo = szValue;
                }
                break;
            }
        }
    }

    @Override
    public boolean CancelReturn() {
        Log.i("test","返回-->"+mItems.size());
        Intent intent = new Intent();

        Bundle bundle = new Bundle();

        bundle.putString("returnValue", JSON.toJSONString(mItems));

        intent.putExtras(bundle);

        // 返回
        setResult(RESULT_OK, intent);

        return super.SureReturn();
    }

    /****
     * 提交返回
     * @return
     */
    @Override
    public boolean SureReturn() {
        return false;
    }

    @Override
    public void onBackPressed() {
        CancelReturn();
    }
}
